package toy.keli.picmap.tools;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import java.util.ArrayList;

import toy.keli.picmap.model.def;

//离线地图  https://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-map/offlinemap
public class OfflineMap {
    MKOfflineMap mOffline;
    ProgressDialog mProgress;
    def hasAll=new def();
    public boolean status=false;
    public String cityCode;
    public OfflineMap(){
        mOffline = new MKOfflineMap();
        mOffline.init(new MKOfflineMapListener() {
            @Override
            public void onGetOfflineMapState(int i, int i1) {

            }
        });
    }
    public OfflineMap(Context context, BDLocation location){
        mOffline = new MKOfflineMap();
        //进度对话框https://www.yiibai.com/android/android_progressbar.html
        mProgress=new ProgressDialog(context);
        mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgress.setMax(100);
        try {
            ArrayList<MKOLSearchRecord> all = mOffline.getOfflineCityList();
            for(int i=0;i<all.size();i++){
                MKOLSearchRecord r = all.get(i);
                hasAll.set(r.cityID,r);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        cityCode=location.getCityCode();
        status=cityCode!=null ? !hasAll.get(cityCode).isEmpyt(): true;
        // 传入MKOfflineMapListener，离线地图状态发生改变时会触发该回调
        mOffline.init(new MKOfflineMapListener() {
            /**
             * @param type  事件类型: TYPE_NEW_OFFLINE, TYPE_DOWNLOAD_UPDATE, TYPE_VER_UPDATE.
             * @param state  事件状态: 当type为TYPE_NEW_OFFLINE时，表示新安装的离线地图数目.当type为TYPE_VER_UPDATE时，表示更新的城市ID.
             * @link https://tieba.baidu.com/p/1742575270?red_tag=1473504030
             */
            @Override
            public void onGetOfflineMapState(int type, int state) {
                switch (type) {
                    case MKOfflineMap.TYPE_DOWNLOAD_UPDATE:     //离线地图下载更新
                    {
                        MKOLUpdateElement update = mOffline.getUpdateInfo(state);
                        mProgress.setMessage("下载"+update.cityName+"地图...");
                        mProgress.setProgress(update.ratio);
                        if(update.ratio==100) mProgress.hide();
                    }
                    break;
                    case MKOfflineMap.TYPE_NEW_OFFLINE:     //离线地图数据版本更新
                        Log.d("OfflineDemo", String.format("add offlinemap num:%d", state));
                        break;
                    case MKOfflineMap.TYPE_VER_UPDATE://新安装离线地图
                        Log.d("OfflineDemo", String.format("new offlinemap ver"));
                        break;
                }
            }
        });
    }
    public def cityList(){
        return cityList(true);
    }
    public def cityList(boolean hot){
        def rt=new def();
        ArrayList<MKOLSearchRecord> all = hot ? mOffline.getHotCityList() : mOffline.searchCity("");
        for(int i=0;i<all.size();i++){
            MKOLSearchRecord r = all.get(i);
            rt.set(r.cityID,r.cityName);
        }
        return rt;
    }

    public void downloadCity(int cityid) {
        if(cityid==0) return;
        mProgress.setTitle("离线地图下载");
        mProgress.setMessage("下载地图...");
        mProgress.show();
        mOffline.start(cityid);
    }

    public boolean removeCity(int cityid){
        mOffline.remove(cityid);
        return true;
    }
    public boolean updateCity(int cityid){
        mOffline.update(cityid);
        return true;
    }


    public int getCityIdByName(String cityName){
        int cityId=0;
        ArrayList<MKOLSearchRecord> records = mOffline.searchCity(cityName);
        if (records != null && records.size() == 1) {
            cityId = records.get(0).cityID;
        }
        return cityId;
    }
}
